package net.lab1024.sa.admin.module.system.datascope.service;

import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.lab1024.sa.admin.module.system.datascope.constant.DataScopeTypeEnum;
import net.lab1024.sa.admin.module.system.datascope.constant.DataScopeViewTypeEnum;
import net.lab1024.sa.admin.module.system.datascope.domain.DataScopeSqlConfig;
import net.lab1024.sa.admin.module.system.datascope.strategy.AbstractDataScopeStrategy;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：zhanghao
 * @name：DataScopeCustomService
 * @Date：2024/8/21 9:55
 */
@Service
public class DataScopeCustomService extends AbstractDataScopeStrategy {
    private static final String EMPLOYEE_PARAM = "#employeeIds";

    private static final String DEPARTMENT_PARAM = "#departmentIds";

    @Resource
    private DataScopeViewService dataScopeViewService;

    @Override
    public String getCondition(DataScopeViewTypeEnum viewTypeEnum, Map<String, Object> paramMap, DataScopeSqlConfig sqlConfigDTO) {
        System.out.println(JSONUtil.toJsonStr(paramMap));
        DataScopeTypeEnum dataScopeTypeEnum = sqlConfigDTO.getDataScopeType();
        String deptSql = "";
        String userSql = "";
        String params = sqlConfigDTO.getParamName();

        Map pmap = this.parseJson(params);

        if (null != pmap) {
            if (null != pmap.get("user")) {
                userSql = pmap.get("user").toString() + " IN (" + EMPLOYEE_PARAM + ")";
            }
            if (null != pmap.get("dept")) {
                deptSql = pmap.get("dept").toString() + " IN (" + DEPARTMENT_PARAM + ")";
            }

        }

        Long employeeId = SmartRequestUtil.getRequestUserId();
        if (employeeId == null) {
            return "";
        }

        if (DataScopeViewTypeEnum.DEPARTMENT == viewTypeEnum || DataScopeViewTypeEnum.DEPARTMENT_AND_SUB == viewTypeEnum) {
            List<Long> canViewDepartmentIds = dataScopeViewService.getCanViewDepartmentId(dataScopeTypeEnum, employeeId);
            if (CollectionUtils.isEmpty(canViewDepartmentIds)) {
                return "";
            }
            String departmentIds = StringUtils.join(canViewDepartmentIds, ",");
            String sql = deptSql.replaceAll(DEPARTMENT_PARAM, departmentIds);
            return sql;
        }
        if (DataScopeViewTypeEnum.ME == viewTypeEnum) {
            List<Long> canViewEmployeeIds = dataScopeViewService.getCanViewEmployeeId(dataScopeTypeEnum, employeeId);
            if (CollectionUtils.isEmpty(canViewEmployeeIds)) {
                return "";
            }
            String employeeIds = StringUtils.join(canViewEmployeeIds, ",");
            String sql = userSql.replaceAll(EMPLOYEE_PARAM, employeeIds);
            return sql;
        }

        return "";
    }
    public Map<String, Object> parseJson(String jsonStr) {
        Map<String, Object> map = new HashMap<>();
        jsonStr = jsonStr.substring(1, jsonStr.length() - 1); // 去除首尾大括号
        String[] pairs = jsonStr.split(",");
        for (String pair : pairs) {
            String[] keyValue = pair.split(":");
            map.put(keyValue[0].trim(), keyValue[1].trim());
        }
        return map;
    }


}
